package com.iwop.moto.controller.web;

import com.iwop.moto.comm.Comm;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.IFGoodsService;
import com.iwop.moto.service.IFMotoGeneralService;
import com.iwop.moto.service.impl.FMotoParamServiceImpl;
import com.iwop.moto.utils.file.FileUtil;
import com.iwop.moto.utils.stringUtil.ObjectId;
import org.omg.IOP.CodecOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * 功能: 摩配整车信息管理
 * 说明: 新增/修改/查询
 * 数据表: F_MOTO
 *
 * @author Created by liuwb on 2016-07-15.
 */
@Controller
public class MotoController extends BaseController {

    public static final String ImageFilePath = "image/dev-pic/";

    @Autowired
    private IFGoodsService fgoodsService;
    @Autowired
    private IFMotoGeneralService fMotoGeneralService;
    @Autowired
    private FMotoParamServiceImpl fMotoParamService;

    /**
     * 功能说明:整车信息 - 列表
     */
    @RequestMapping(value = "/moto/list")
    @ResponseBody
    public ModelAndView motolist() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        try {
            if (SRModel.get("currentPage") == null || Integer.valueOf(SRModel.get("currentPage").toString()) <= 1) {
                SRModel.put("currentResult", 0);
                SRModel.put("currentPage", 1);
            } else {
                SRModel.put("currentResult", (Integer.valueOf(SRModel.get("currentPage").toString()) - 1) * Integer.valueOf(SRModel.get("showCount").toString()));
            }
            if (SRModel.get("showCount") == null) {
                SRModel.put("showCount", 15);
            } else {
                SRModel.put("showCount", Integer.valueOf(SRModel.get("showCount").toString()));
            }
            List<SuperRecordModel> srm = fgoodsService.searchFmotoList(SRModel);
            SuperRecordModel srmTotal = fgoodsService.findFmotoTotalPage(SRModel);
            SRModel.put("totalPage", srmTotal.get("totalPage"));
            SRModel.put("totalCount", srmTotal.get("totalCount"));
            mv.setViewName("moto_list");
            mv.addObject("srm", srm);
            mv.addObject("SRModel", SRModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 功能说明：部件信息
     *
     * @return
     */
    @RequestMapping(value = "/part/list")
    @ResponseBody
    public ModelAndView partList() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        try {
            if (SRModel.get("currentPage") == null || Integer.valueOf(SRModel.get("currentPage").toString()) <= 1) {
                SRModel.put("currentResult", 0);
                SRModel.put("currentPage", 1);
            } else {
                SRModel.put("currentResult", (Integer.valueOf(SRModel.get("currentPage").toString()) - 1) * Integer.valueOf(SRModel.get("showCount").toString()));
            }
            if (SRModel.get("showCount") == null) {
                SRModel.put("showCount", 15);
            } else {
                SRModel.put("showCount", Integer.valueOf(SRModel.get("showCount").toString()));
            }
            List<SuperRecordModel> srm = fMotoGeneralService.searchMotoGeneralList(SRModel);
            SuperRecordModel srmTotal = fMotoGeneralService.findMotoGeneralTotalPage(SRModel);

            SRModel.put("totalPage", srmTotal.get("totalPage"));
            SRModel.put("totalCount", srmTotal.get("totalCount"));
            mv.addObject("SRModel", SRModel);
            mv.addObject("srm", srm);
            mv.setViewName("part_list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 功能说明:整车信息 - 编辑信息
     * liuwb 2016-07-15
     */
    @RequestMapping(value = "/moto/edit")
    @ResponseBody
    public ModelAndView motoadd() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        if (!"".equals(SRModel.getString("sid"))) {
            SRModel = fgoodsService.findFmotoBySid(SRModel);
        }
        mv.setViewName("moto_edit");
        mv.addObject("SRModel", SRModel);
        return mv;
    }


    /**
     * 功能说明:摩配信息 - 编辑保存
     */
    @RequestMapping(value = "/moto/save")
    public ModelAndView motosave(
            @RequestParam(value = "sid", required = false) String sid,
            @RequestParam(value = "moto_name", required = false) String moto_name,
            @RequestParam(value = "moto_type", required = false) String moto_type,
            @RequestParam(value = "moto_brand", required = false) String moto_brand,
            @RequestParam(value = "moto_price", required = false) String moto_price,
            @RequestParam(value = "moto_url", required = false) String moto_url,
            @RequestParam(value = "img_1", required = false) MultipartFile img_1,
            @RequestParam(value = "img_url", required = false) String img_url,
            @RequestParam(value = "cs_ckg", required = false) String cs_ckg,
            @RequestParam(value = "cs_zj", required = false) String cs_zj,
            @RequestParam(value = "cs_zdg", required = false) String cs_zdg,
            @RequestParam(value = "cs_ldjx", required = false) String cs_ldjx,
            @RequestParam(value = "cs_kczl", required = false) String cs_kczl,
            @RequestParam(value = "cs_zczl", required = false) String cs_zczl,
            @RequestParam(value = "cs_ls", required = false) String cs_ls,
            @RequestParam(value = "cs_lw", required = false) String cs_lw,
            @RequestParam(value = "cs_qlgg", required = false) String cs_qlgg,
            @RequestParam(value = "cs_hlgg", required = false) String cs_hlgg,
            @RequestParam(value = "cs_yxrj", required = false) String cs_yxrj,
            @RequestParam(value = "cs_kxys", required = false) String cs_kxys,
            @RequestParam(value = "fdj_xh", required = false) String fdj_xh,
            @RequestParam(value = "fdj_rylx", required = false) String fdj_rylx,
            @RequestParam(value = "fdj_gs", required = false) String fdj_gs,
            @RequestParam(value = "fdj_cc", required = false) String fdj_cc,
            @RequestParam(value = "fdj_lqfs", required = false) String fdj_lqfs,
            @RequestParam(value = "fdj_zdgl", required = false) String fdj_zdgl,
            @RequestParam(value = "fdj_pl", required = false) String fdj_pl,
            @RequestParam(value = "fdj_ysb", required = false) String fdj_ysb,
            @RequestParam(value = "fdj_zdnj", required = false) String fdj_zdnj,
            @RequestParam(value = "fdj_gyfs", required = false) String fdj_gyfs,
            @RequestParam(value = "fdj_jjyh", required = false) String fdj_jjyh,
            @RequestParam(value = "fdj_hbbz", required = false) String fdj_hbbz,
            @RequestParam(value = "qt_zdfs", required = false) String qt_zdfs,
            @RequestParam(value = "qt_qdfs", required = false) String qt_qdfs,
            @RequestParam(value = "qt_lhqxs", required = false) String qt_lhqxs,
            @RequestParam(value = "qt_bsqxs", required = false) String qt_bsqxs,
            @RequestParam(value = "qt_zczb", required = false) String qt_zczb,
            @RequestParam(value = "qt_zgss", required = false) String qt_zgss,
            @RequestParam(value = "qt_zdzz", required = false) String qt_zdzz,
            @RequestParam(value = "pz_qdd", required = false) String pz_qdd,
            @RequestParam(value = "pz_hwd", required = false) String pz_hwd,
            @RequestParam(value = "pz_gzpz", required = false) String pz_gzpz,
            @RequestParam(value = "pz_zzq", required = false) String pz_zzq,
            @RequestParam(value = "pz_xsq", required = false) String pz_xsq,
            @RequestParam(value = "pz_ybp", required = false) String pz_ybp,
            @RequestParam(value = "pz_cwx", required = false) String pz_cwx,
            @RequestParam(value = "pz_lt", required = false) String pz_lt,
            @RequestParam(value = "pz_qt", required = false) String pz_qt,
            @RequestParam(value = "moto_remark", required = false) String moto_remark

    ) {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("status_time", new Date());
        SRModel.put("sid", sid);
        SRModel.put("moto_name", moto_name);
        SRModel.put("moto_type", moto_type);
        SRModel.put("moto_brand", moto_brand);
        SRModel.put("moto_price", moto_price);
        SRModel.put("moto_url", moto_url);
        SRModel.put("cs_ckg", cs_ckg);
        SRModel.put("cs_zj", cs_zj);
        SRModel.put("cs_zdg", cs_zdg);
        SRModel.put("cs_ldjx", cs_ldjx);
        SRModel.put("cs_kczl", cs_kczl);
        SRModel.put("cs_zczl", cs_zczl);
        SRModel.put("cs_ls", cs_ls);
        SRModel.put("cs_lw", cs_lw);
        SRModel.put("cs_qlgg", cs_qlgg);
        SRModel.put("cs_hlgg", cs_hlgg);
        SRModel.put("cs_yxrj", cs_yxrj);
        SRModel.put("cs_kxys", cs_kxys);
        SRModel.put("fdj_xh", fdj_xh);
        SRModel.put("fdj_rylx", fdj_rylx);
        SRModel.put("fdj_gs", fdj_gs);
        SRModel.put("fdj_cc", fdj_cc);
        SRModel.put("fdj_lqfs", fdj_lqfs);
        SRModel.put("fdj_zdgl", fdj_zdgl);
        SRModel.put("fdj_pl", fdj_pl);
        SRModel.put("fdj_ysb", fdj_ysb);
        SRModel.put("fdj_zdnj", fdj_zdnj);
        SRModel.put("fdj_gyfs", fdj_gyfs);
        SRModel.put("fdj_jjyh", fdj_jjyh);
        SRModel.put("fdj_hbbz", fdj_hbbz);
        SRModel.put("qt_zdfs", qt_zdfs);
        SRModel.put("qt_qdfs", qt_qdfs);
        SRModel.put("qt_lhqxs", qt_lhqxs);
        SRModel.put("qt_bsqxs", qt_bsqxs);
        SRModel.put("qt_zczb", qt_zczb);
        SRModel.put("qt_zgss", qt_zgss);
        SRModel.put("qt_zdzz", qt_zdzz);
        SRModel.put("pz_qdd", pz_qdd);
        SRModel.put("pz_hwd", pz_hwd);
        SRModel.put("pz_gzpz", pz_gzpz);
        SRModel.put("pz_zzq", pz_zzq);
        SRModel.put("pz_xsq", pz_xsq);
        SRModel.put("pz_ybp", pz_ybp);
        SRModel.put("pz_cwx", pz_cwx);
        SRModel.put("pz_lt", pz_lt);
        SRModel.put("pz_qt", pz_qt);
        SRModel.put("moto_remark", moto_remark);
        if (img_url != null && !"".equals(img_url.trim())) {
            SRModel.put("img_url", img_url);
        }
        if (img_1 != null && !"".equals(img_1.getOriginalFilename())) {
            SRModel.put("img_url", FileUtil.uploadImg(img_1, Comm.ROOT_PATH));
        }
        try {
            if (sid != null && !"".equals(sid)) {
                fgoodsService.updeteSelective(SRModel);
            } else {
                SRModel.put("sid", ObjectId.userUUID());
                fgoodsService.insertMotoSelective(SRModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.setViewName("moto_edit");
        mv.addObject("SRModel", SRModel);
        return mv;
    }

    /**
     * 功能说明:ajax更新状态
     */
    @RequestMapping(value = "/ajax/moto/status")
    @ResponseBody
    public ResultModel ajaxUpMotoStatus() {
        ResultModel resultModel = new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        fgoodsService.updateFMotoStatus(SRModel);
        resultModel.setCode(0);
        resultModel.setMem("成功");
        return resultModel;
    }

    /**
     * 功能:校验名称是否存在
     */
    @RequestMapping(value = "/ajax/ck/name")
    @ResponseBody
    public ResultModel ajaxCkName() {
        ResultModel resultModel = new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("moto_name", SRModel.getString("moto_name").trim());
            SRModel = fgoodsService.findFmotoByName(SRModel);
            if (Integer.valueOf(SRModel.get("totalCount").toString()) == 0) {
                resultModel.setCode(0);
                resultModel.setMem("成功");
            } else {
                resultModel.setCode(1);
                resultModel.setMem("已存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    /**
     * 功能:校验URL是否存在
     */
    @RequestMapping(value = "/ajax/ck/url")
    @ResponseBody
    public ResultModel ajaxCkUrl() {
        ResultModel resultModel = new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("moto_url", SRModel.getString("moto_url").trim());
            SRModel = fgoodsService.findFmotoByUrl(SRModel);
            if (Integer.valueOf(SRModel.get("totalCount").toString()) == 0) {
                resultModel.setCode(0);
                resultModel.setMem("成功");
            } else {
                resultModel.setCode(1);
                resultModel.setMem("已存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultModel;
    }


    /**
     * 功能说明：部件信息 - 保存
     *
     * @return
     */
    @RequestMapping(value = "/part/edit")
    @ResponseBody
    public ModelAndView partEdit() {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = this.getSRModel();
        List<SuperRecordModel> paramList = null;
        String param_fkey = (String) SRModel.get("sid");
        if (!"".equals(SRModel.getString("sid"))) {
            SRModel = fMotoGeneralService.findFmotoBySid(SRModel);
            paramList = fMotoParamService.findMotoParamByParamKey(param_fkey);
        }
        mv.addObject("SRModel", SRModel);
        mv.addObject("paramList", paramList);
        mv.setViewName("part_edit");
        return mv;
    }

    /**
     * 功能说明:摩配信息 - 编辑保存
     */
    @RequestMapping(value = "/part/save")
    public ModelAndView partSave(
            @RequestParam(value = "sid", required = false) String sid,
            @RequestParam(value = "moto_name", required = false) String moto_name,
            @RequestParam(value = "moto_type", required = false) String moto_type,
            @RequestParam(value = "moto_two_type", required = false) String moto_two_type,
            @RequestParam(value = "moto_brand", required = false) String moto_brand,
            @RequestParam(value = "moto_model", required = false) String moto_model,
            @RequestParam(value = "moto_url", required = false) String moto_url,
            @RequestParam(value = "img_1", required = false) MultipartFile img_1,
            @RequestParam(value = "img_url", required = false) String img_url,
            @RequestParam(value = "rkeys_str", required = false) String rkeys_str,
            @RequestParam(value = "rvals_str", required = false) String rvals_str

    ) {
        ModelAndView mv = new ModelAndView();
        SuperRecordModel SRModel = new SuperRecordModel();
        SRModel.put("status_time", new Date());
        SRModel.put("sid", sid);
        SRModel.put("moto_name", moto_name);
        SRModel.put("moto_type", moto_type);
        SRModel.put("moto_two_type", moto_two_type);
        SRModel.put("moto_brand", moto_brand);
        SRModel.put("moto_model", moto_model);
        SRModel.put("moto_url", moto_url);
        SRModel.put("status_flag", 1);

        SuperRecordModel SRModel_ = new SuperRecordModel();
        SRModel_.put("rkeys_str", rkeys_str);
        SRModel_.put("sid", sid);
        SRModel_.put("rvals_str", rvals_str);
        SRModel_.put("status_flag", 1);

        if (img_url != null && !"".equals(img_url.trim())) {
            SRModel.put("img_url", img_url);
        }
        if (img_1 != null && !"".equals(img_1.getOriginalFilename())) {
            SRModel.put("img_url", FileUtil.uploadImg(img_1, Comm.ROOT_PATH));
        }

        try {
            if (sid != null && !"".equals(sid)) {
                fMotoGeneralService.updeteSelective(SRModel);
                paramInsertAndModify(SRModel_);
            } else {
                String sidNew = ObjectId.userUUID();
                SRModel.put("sid", sidNew);
                fMotoGeneralService.insertMotoSelective(SRModel);
                SRModel_.put("sid", sidNew);
                paramInsertAndModify(SRModel_);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<SuperRecordModel> paramList = null;
        String param_fkey = (String) SRModel.get("sid");
        if (!"".equals(SRModel.getString("sid"))) {
            SRModel = fMotoGeneralService.findFmotoBySid(SRModel);
            paramList = fMotoParamService.findMotoParamByParamKey(param_fkey);
        }
        mv.addObject("SRModel", SRModel);
        mv.addObject("paramList", paramList);
        mv.setViewName("part_edit");

        return mv;
    }

    public void paramInsertAndModify(SuperRecordModel superRecordModel) {
        String rkeys_str = (String) superRecordModel.get("rkeys_str");
        String rvals_str = (String) superRecordModel.get("rvals_str");
        String sid = (String) superRecordModel.get("sid");
        String[] keys = rkeys_str.split(",");
        String[] values = rvals_str.split(",");
        SuperRecordModel srm;
        fMotoParamService.deleteByParamFkey(sid);//删除所有
        for (int i = 0; i < keys.length; i++) {
            srm = new SuperRecordModel();
            srm.put("param_fkey", sid);
            srm.put("param_name", keys[i]);
            srm.put("param_value", values[i]);
            try {
                srm.put("sid", ObjectId.userUUID());
                fMotoParamService.insertSelective(srm);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 功能说明:ajax更新状态
     */
    @RequestMapping(value = "/ajax/part/status")
    @ResponseBody
    public ResultModel ajaxUpPartStatus() {
        ResultModel resultModel = new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        fMotoParamService.updatePartStatus(SRModel);
        resultModel.setCode(0);
        resultModel.setMem("成功");
        return resultModel;
    }

    @RequestMapping(value = "/ajax/part/list/status")
    @ResponseBody
    public ResultModel ajaxUpPartListStatus() {
        ResultModel resultModel = new ResultModel();
        SuperRecordModel SRModel = this.getSRModel();
        fMotoGeneralService.updatePartStatus(SRModel);
        resultModel.setCode(0);
        resultModel.setMem("成功");
        return resultModel;
    }


}
